﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuizAppMvc2.Models;

namespace QuizAppMvc2.Controllers
{
    public class TopicoController : Controller
    {
        //
        // GET: /Topico/
        quizdbEntities _db = new quizdbEntities();

        public ActionResult Index()
        {
            return View(_db.Topicoes);
        }

        //
        // GET: /Topico/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Topico/Create

        public ActionResult Create()
        {
            TopicoModels m = new TopicoModels();

            m.materias = _db.Materias.ToList<Materia>();

            return View(m);
        } 

        //
        // POST: /Topico/Create

        [HttpPost]
        public ActionResult Create(TopicoModels collection)
        {
            try
            {
                Topico topico = new Topico();

                topico.idMateria = collection.idMateria;
                topico.Nome = collection.texto;
                _db.AddToTopicoes(topico);
                _db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Topico/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Topico/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Topico/Delete/5
 
        public ActionResult Delete(int id)
        {
            var toDelete = (from m in _db.Topicoes

                                   where m.idTopico == id

                                   select m).First();
            return View(toDelete);
            
        }

        //
        // POST: /Topico/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var toDelete = (from m in _db.Topicoes

                                where m.idTopico == id

                                select m).First();
                _db.DeleteObject(toDelete);
                _db.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult abc(int year)
        {
            var v = _db.Topicoes.Where(x => x.idMateria == year);
            //var v = db.Topicoes;

            return Json(
                v.Select(x => new { value = x.idTopico, novo = x.Nome }),
                JsonRequestBehavior.AllowGet
            );
        }
    }
}
